package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.QuestionItem;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.QuestionItemService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@WebServlet("/store/questionItem/*")
public class QuestionItemServlet extends BaseServlet {

private QuestionItemService questionItemService = BeanFactory.getService("questionItemService", QuestionItemService.class);


    private  Result findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        try {
            //1 获取问题id
            String questionId = req.getParameter("questionId");
            //2 调用service层方法获取分页结果
            List<QuestionItem> list = questionItemService.findAll(questionId);
            // 3 对客户的作出响应
            return new Result(true,MessageConstant.QUERY_QUESTIONITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_QUESTIONITEM_FAIL);
        }
    }

    private Result delete(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数并封装成QuestionItem对象
            String id = req.getParameter("id");
            //2 调用service层方法添加数据
            questionItemService.delete(id);
            //3 响应结果
            return new Result(true,MessageConstant.DELETE_QUESTIONITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_QUESTIONITEM_FAIL);
        }
    }

    private Result edit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数并封装成QuestionItem对象
            QuestionItem questionItem = BeanUtil.fillBeanFromJson(req, QuestionItem.class);
            //2 调用service层方法添加数据
            questionItemService.update(questionItem);
            //3 响应结果
            return new Result(true,MessageConstant.EDIT_QUESTIONITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_QUESTIONITEM_FAIL);
        }
    }

    private Result findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数
            String id = req.getParameter("id");
            //2 调用service层方法获取分页结果
            QuestionItem questionItem = questionItemService.findById(id);
            //3 对客户的作出响应
            return new Result(true,MessageConstant.QUERY_QUESTIONITEM_SUCCESS,questionItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_QUESTIONITEM_FAIL);
        }
    }

    private Result save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数并封装成QuestionItem对象
            QuestionItem questionItem = BeanUtil.fillBeanFromJson(req, QuestionItem.class);
            //2 调用service层方法添加数据
            questionItemService.save(questionItem);
            //3 响应结果
            return new Result(true,MessageConstant.SAVE_QUESTIONITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_QUESTIONITEM_FAIL);
        }
    }

//    @Override
//    protected Result doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, Exception {
//        doGet(req, resp);
//    }
}
